
package pions.view.employees;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import org.w3c.dom.Document;
import pions.controller.Employees;
import pions.view.PIONSView;

/**
 *
 * 
 */
public class SubordinatesEList extends javax.swing.JPanel {

    private DisplayEmployee display_employee = null;
    private ArrayList<String> names = null;
    private ArrayList<JRadioButton> radio_buttons = null;
    private ArrayList<JTextArea> text_areas = null;
    private ButtonGroup button_group = new ButtonGroup();

    /** Creates new form SubordinateList */
    public SubordinatesEList() {
        initComponents();

        radio_buttons = new ArrayList<JRadioButton>();
        text_areas = new ArrayList<JTextArea>();

        display();
    }

    private DisplayEmployee getDisplayEmployee(int index){
        Document xml = Employees.getSubordinateXML(index);

        if(display_employee == null){
            display_employee = new DisplayEmployee(xml);
        }
        else {
            display_employee.set(xml);
        }

        return display_employee;
    }

    private void display(){
        GridBagLayout layout = new GridBagLayout();

        //TODO Sort the contents of names without affecting the order of buttons
        for(String name: Employees.getSubordinates()){
            GridBagConstraints constraints = new GridBagConstraints();
            constraints.weightx = .5;
            constraints.weighty = 0;
            constraints.insets = new Insets(0,0,10,0);
            constraints.anchor = GridBagConstraints.PAGE_START;
            constraints.gridy = GridBagConstraints.RELATIVE;

            JRadioButton radio_button = new JRadioButton();
            constraints.gridx = 0;
            constraints.fill = GridBagConstraints.BOTH;
            layout.setConstraints(radio_button, constraints);

            JTextArea text_area = new JTextArea(name);
            text_area.setEditable(false);
            text_area.setLineWrap(true);
            text_area.setWrapStyleWord(true);
            constraints.gridx = 1;
            constraints.fill = GridBagConstraints.HORIZONTAL;
            layout.setConstraints(text_area, constraints);

            panel_display.add(radio_button);
            panel_display.add(text_area);

            radio_button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    PIONSView.getInstance().setAux(getDisplayEmployee(radio_buttons.indexOf(evt.getSource())));
                }
            });
            button_group.add(radio_button);
            radio_buttons.add(radio_button);
            text_areas.add(text_area);
        }

        panel_display.setLayout(layout);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label_title = new javax.swing.JLabel();
        label_directions = new javax.swing.JLabel();
        scrollpane_display = new javax.swing.JScrollPane();
        panel_display = new javax.swing.JPanel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pions.PIONS.class).getContext().getResourceMap(SubordinatesEList.class);
        label_title.setFont(resourceMap.getFont("label_title.font")); // NOI18N
        label_title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        label_title.setText(resourceMap.getString("label_title.text")); // NOI18N
        label_title.setName("label_title"); // NOI18N

        label_directions.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        label_directions.setText(resourceMap.getString("label_directions.text")); // NOI18N
        label_directions.setName("label_directions"); // NOI18N

        scrollpane_display.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollpane_display.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollpane_display.setName("scrollpane_display"); // NOI18N

        panel_display.setName("panel_display"); // NOI18N
        panel_display.setPreferredSize(new java.awt.Dimension(100, 100));

        javax.swing.GroupLayout panel_displayLayout = new javax.swing.GroupLayout(panel_display);
        panel_display.setLayout(panel_displayLayout);
        panel_displayLayout.setHorizontalGroup(
            panel_displayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 261, Short.MAX_VALUE)
        );
        panel_displayLayout.setVerticalGroup(
            panel_displayLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 298, Short.MAX_VALUE)
        );

        scrollpane_display.setViewportView(panel_display);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(label_title, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            .addComponent(label_directions, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollpane_display, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(label_title)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(label_directions)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollpane_display, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel label_directions;
    private javax.swing.JLabel label_title;
    private javax.swing.JPanel panel_display;
    private javax.swing.JScrollPane scrollpane_display;
    // End of variables declaration//GEN-END:variables

}
